package org.hazelnuts.util;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.io.*;

public class CodeTest {
    public void testSendAndReceive() {
        //创建ConnectionFactory //注意: guest的用户只能够在localhost 127.0.0.1进行测试
        String hostname = "10.10.10.133";
        String username = "admin";
        String password = "admin";
        String virtualHost = "/";
        CachingConnectionFactory cf = new CachingConnectionFactory(hostname);
        cf.setUsername(username);
        cf.setPassword(password);
        cf.setVirtualHost(virtualHost);

//        RabbitAdmin admin = new RabbitAdmin(cf);

        //创建Exchange
        String exchangeName = "direct.order.exchange";
//        DirectExchange exchange = new DirectExchange(exchangeName);
//        admin.declareExchange(exchange);

        //创建Queue
        String queueName = "direct.order.queue";
//        Queue queue = new Queue(queueName, true, false, false, null);
//        admin.declareQueue(queue);

        //创建Binding
        String routingKey = "direct.order.queue";
//        admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with(routingKey));

        //创建RabbitTemplate
        RabbitTemplate rabbitTemplate = new RabbitTemplate(cf);
        rabbitTemplate.setExchange(exchangeName);
        rabbitTemplate.setDefaultReceiveQueue(queueName);

        boolean isStop = false;
        File directory = new File("C:\\Users\\WN-Time\\Desktop\\csv\\test-sql");
        if (!directory.isDirectory()) {
            return;
        }
        File[] files = directory.listFiles();
        if (files.length == 0) {
            return;
        }
        int index = 0;
        while (!isStop) {
            try {
                //for (File file : files) {
                File file = files[index];
                String lineSql = "";
                if (file != null && file.isFile()) {
                    FileReader fileReader = new FileReader(file);
                    BufferedReader bufferedReader = new BufferedReader(fileReader);
                    while ((lineSql = bufferedReader.readLine()) != null) {
                        Message message = MessageBuilder.withBody(lineSql.getBytes("UTF-8"))
                                .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN).build();
                        //根据routingKey发送消息
                        System.out.println("message=" + message);
                        rabbitTemplate.send(routingKey, message);
                    }
                    //}
                }
            } catch (FileNotFoundException exception) {
                System.out.println(exception.getMessage());
            } catch (IOException exception) {
                System.out.println(exception.getMessage());
            }
        }

        //接收消息
//        Message resultMessage = rabbitTemplate.receive();
//        System.out.println("resultMessage=" + resultMessage);
//        if (resultMessage != null) {
//            try {
//                System.out.println("receive massage=" + new String(resultMessage.getBody(), "UTF-8"));
//            } catch (UnsupportedEncodingException exception) {
//                System.out.println(exception.getMessage());
//            }
//        }
    }
}
